Extensible Pattern Matching in an Extensible Language

نویسنده

  • Sam Tobin-Hochstadt
چکیده

Pattern matching is a widely used technique in functional languages, especially those in the ML and Haskell traditions. Although pattern matching is typically not built into languages in the Lisp tradition, it is often available via libraries built with macros. We present a sophisticated pattern matcher for Racket, which extends the language using macros, supports novel and widelyuseful pattern-matching forms, and is itself extensible with macros. 1 Extending Pattern Matching The following Racket1 [3] program finds the magnitude of a complex number, represented in either Cartesian or polar form as a 3-element list: (define (magnitude n) (cond [(eq? (first n) ’cart) (sqrt (+ (sqr (second n)) (sqr (third n))))] [(eq? (first n) ’polar) (second n)])) While this program accomplishes the desired purpose, it’s far from obviously correct, and commits the program to the list-based representation. Additionally, it unnecessarily repeats accesses to the list structure making up the representation. Finally, if the input is ’(cart 7), it produces a hard-to-decipher error. In contrast, the same program written using pattern matching is far easier to understand: (define (magnitude n) (match n [(list ’cart x y) (sqrt (+ (sqr x) (sqr y)))] [(list ’polar r theta) r])) The new program is shorter, more perspicuous, does not repeat computation, and produces better error messages. The function can also be easily converted to arbitrary-dimensional coordinates: (define (magnitude n) (match n [(list ’cart xs ...) (sqrt (apply + (map sqr xs)))] [(list ’polar r theta ...) r])) 1 Racket is the new name of PLT Scheme. This definition is much improved from the original, but it still commits us to a list-based representation of coordinates. By switching to custom, user-defined pattern matching forms, this representation choice can be abstracted away: (define (magnitude n) (match n [(cart xs ...) (sqrt (apply + (map sqr xs)))] [(polar r theta ...) r])) Our custom pattern matching form can use other features of Racket’s pattern matcher to perform arbitrary computation, allowing us to simplify the function further by transparently converting Cartesian to polar coordinates when necessary: (define (magnitude n) (match n [(polar r theta ...) r])) In the remainder of the paper, we describe the implementation of all of these examples, focusing on user-extensibility. We begin with a history of pattern matching in Scheme, leading up to the current implementation in Racket, touching briefly on the adaptation of standard techniques from ML-style matching [4] and their implementation via macros [2,1]. Then we describe the implementation of sequence patterns (seen above with the use of ...) and other pattern forms not found in conventional patternmatching systems. Third, we describe how to make patterns user-extensible by exploiting the flexibility of Racket’s macro system.

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

Pattern matching in concatenative programming languages

Pattern matching is a useful, convenient tool for processing algebraic data types in functional programming languages. This paper discusses a method for porting pattern matching to a family of programming languages called concatenative, which does not use named variables. The system is not fixed, like a traditional pattern matching system, but rather arbitrarily extensible: any function can be ...

متن کامل

Twig Pattern Matching Algorithms for XML

The emergence of XML promised significant advances in B2B integration. This is because users can store or transmit structure data using this highly flexible open standard. An effective well-formed XML document structure helps convert data into useful information that can be processed quickly and efficiently. From this point there is need for efficient processing of queries on XML data in XML da...

متن کامل

XMLSpaces.NET: An Extensible Tuplespace as XML Middleware

XMLSpaces.NET implements the Linda concept as a middleware for XML documents on the .NET platform. It introduces an extended matching flexibility on nested tuples and richer data types for fields, including objects and XML documents. It is completely XML-based since data, tuples and tuplespaces are seen as trees represented as XML documents. XMLSpaces.NET is extensible in that it supports a hie...

متن کامل

An Effective Algorithm for XML Tree Pattern Matching and Pattern Tree Minimization

IJPRES Abstract— The extensible markup language XML has recently to come into read as a new standard for data representation and exchange on the internet. With XML becoming present language for data ability functions in varied domains, efficiently querying XML data is a critical issue. XML has become a follow normal to store, share and exchange business data across similar and dissimilar platfo...

متن کامل

XML Message Filtering and Matching in Publish/Subscribe Systems

.............................................................................................................................. ii Acknowledgements ............................................................................................................ iii List of Figures .................................................................................................................... vi L...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

عنوان ژورنال:
  • CoRR

دوره abs/1106.2578  شماره 

صفحات  -

تاریخ انتشار 2010